import {
  dsx
} from '~/tools/datax'
import {
  ui
} from '~/tools/ui'
import {
  numTool
} from '~/tools/num'
import {
  dateTool
} from '~/tools/date'
import {
  nav2
} from '~/tools/nav2'
import {
  goodsList
} from '~/model/goods'
Page({
  data: {
    staffOpenId: '',
    item: {},
    orders: [],
    unitFee: 3.5
  },
  async onLoad(options) {
    const {
      item,
      staffOpenId,
      unitFee,
      paid
    } = options
    this.setData({
      item: JSON.parse(item),
      staffOpenId,
      unitFee: parseFloat(unitFee),
      paid: paid == 'true'
    })
    // console.log(this.data.item, unitFee, staffOpenId, paid)
    ui.loading()
    await this.loadOrders()
    this.calcFee()
    ui.loading(false)
  },
  async loadOrders() {
    const {
      item,
      staffOpenId,
      paid
    } = this.data
    // console.log(item, staffOpenId)
    const _ = dsx.cmd
    let deliveryFeePaid
    if (paid) deliveryFeePaid = _.eq(true)
    else deliveryFeePaid = _.exists(false).or(_.eq(false))
    const orders = await dsx.getAll('orders', {
      status: _.eq('已送达'),
      deliveryMan: _.eq(staffOpenId),
      deliveryFeePaid,
      createTime: _.gte(item.start).and(_.lte(item.end))
    }, {
      createTime: 1,
      goodsList: 1,
      goodsSnap: 1,
      stepFee: 1
    })
    this.setData({
      orders
    })
    // console.log(this.data.orders)
  },
  calcFee() {
    const {
      orders,
      unitFee
    } = this.data
    for (const order of orders) {
      // console.log(o)
      const bucketNumMap = {} // { fee: num, total: totalNum }
      let bucketNum = 0
      let totalBucketFee = 0
      let title = ''
      const findSnap = function (id) {
        return order.goodsSnap.find(ele => ele._id == id)
      }
      order.goodsList.forEach((goods) => {
        const useFee = findSnap(goods.spuId).deliveryFee || unitFee
        const bucketFee = goods.num * useFee
        totalBucketFee += bucketFee

        bucketNumMap[useFee.toFixed(2)] = (bucketNumMap[useFee.toFixed(2)] ?? 0) + goods.num
        bucketNum += goods.num

        title = goods.title
      })

      const bucketNumList = []
      Object.keys(bucketNumMap).forEach(useFee => {
        bucketNumList.push({
          unit: numTool.maxFixed(useFee, 2),
          num: bucketNumMap[useFee]
        })
      })
      // console.log(bucketNumList)
      order.fee = {
        title,
        date: dateTool.format(order.createTime, "yyyy.MM.dd"),
        stepFee: numTool.maxFixed(order.stepFee, 2),
        num: bucketNum,
        numList: bucketNumList,
        total: numTool.maxFixed(totalBucketFee + order.stepFee, 2)
      }
      // console.log(unitFee, num, o.stepFee)
    }
    this.setData({
      orders
    })
  },
  onCellTap(e) {
    const orderId = ui.eventData(e, 'id')
    nav2.orderList('manage', false, JSON.stringify({
      orderId
    }))
  },
  onReady() {

  },
  onShow() {

  },
  onHide() {

  },

  onUnload() {

  },
  onPullDownRefresh() {

  },


  onReachBottom() {

  },
  onShareAppMessage() {}
})